﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using Zatify.WebUI.Models;

namespace Zatify.WebUI.HtmlHelpers
{
    public static class PagerHelper
    {
        public static MvcHtmlString Pager(this HtmlHelper html, PagingInfo pagingInfo, Func<int, string> func, int displayPagesLinks)
        {
            var result = new StringBuilder();

            var tagParagraph = new TagBuilder("p");
            tagParagraph.AddCssClass("pages");

            var tagSmall = new TagBuilder("small");
            tagSmall.SetInnerText(String.Format("Page {0} of {1}   ", pagingInfo.CurrentPage, pagingInfo.TotalPages));

            tagParagraph.InnerHtml = tagSmall.ToString();


            if (pagingInfo.CurrentPage > 1)
            {
                var tagAnchor = new TagBuilder("a");

                tagAnchor.MergeAttribute("href", func(pagingInfo.CurrentPage - 1));
                tagAnchor.SetInnerText("«");
                tagParagraph.InnerHtml += tagAnchor.ToString();
            }


            //for (int i = 1; i <= displayPagesLinks; i++)
            //{
            //    if (i == pagingInfo.CurrentPage)
            //    {
            //        var tagSpan = new TagBuilder("span");

            //        tagSpan.SetInnerText(i.ToString());
            //        tagParagraph.InnerHtml += tagSpan.ToString();
            //    }
            //    else
            //    {
            //        var tagAnchor = new TagBuilder("a");

            //        tagAnchor.MergeAttribute("href", func(i));
            //        tagAnchor.SetInnerText(i.ToString());
            //        tagParagraph.InnerHtml += tagAnchor.ToString();
            //    }
            //}
            if ((pagingInfo.CurrentPage - 1) % displayPagesLinks == 0)
            {
                for (int i = pagingInfo.CurrentPage; i < (pagingInfo.CurrentPage + displayPagesLinks); i++)
                {
                    /*if (Math.Floor((double)(pagingInfo.TotalItems / pagingInfo.ItemsPerPage) + 1) < i)
                        break;*/
                    if (pagingInfo.TotalPages < i)
                        break;

                    if (i == pagingInfo.CurrentPage)
                    {
                        var tagSpan = new TagBuilder("span");

                        tagSpan.SetInnerText(i.ToString());
                        tagParagraph.InnerHtml += tagSpan.ToString();
                    }
                    else
                    {
                        var tagAnchor = new TagBuilder("a");

                        tagAnchor.MergeAttribute("href", func(i));
                        tagAnchor.SetInnerText(i.ToString());
                        tagParagraph.InnerHtml += tagAnchor.ToString();
                    }

                    if (pagingInfo.CurrentPage == pagingInfo.TotalPages)
                        break;
                }
            }
            else
            {
                var startPageLink = ((pagingInfo.CurrentPage - 1) / displayPagesLinks);

                for (int i = (startPageLink * displayPagesLinks + 1); i <= (startPageLink * displayPagesLinks + displayPagesLinks); i++)
                {
                    /*if (Math.Floor((double)(pagingInfo.TotalItems / pagingInfo.ItemsPerPage) + 1) < i)
                        break;*/
                    if (pagingInfo.TotalPages < i)
                        break;

                    if (i == pagingInfo.CurrentPage)
                    {
                        var tagSpan = new TagBuilder("span");

                        tagSpan.SetInnerText(i.ToString());
                        tagParagraph.InnerHtml += tagSpan.ToString();
                    }
                    else
                    {
                        var tagAnchor = new TagBuilder("a");

                        tagAnchor.MergeAttribute("href", func(i));
                        tagAnchor.SetInnerText(i.ToString());
                        tagParagraph.InnerHtml += tagAnchor.ToString();
                    }


                }
            }

            //if (((pagingInfo.CurrentPage - 1) / displayPagesLinks) < pagingInfo.TotalPages - (pagingInfo.CurrentPage / displayPagesLinks))
            if (pagingInfo.CurrentPage < pagingInfo.TotalPages)
            {
                var tagAnchor = new TagBuilder("a");

                tagAnchor.MergeAttribute("href", func(pagingInfo.CurrentPage + 1));
                tagAnchor.SetInnerText("»");
                tagParagraph.InnerHtml += tagAnchor.ToString();
            }


            result.Append(tagParagraph.ToString());

            return MvcHtmlString.Create(result.ToString());
        }
    }

    //public static class TextTrimerHelper
    //{
    //    public static String TrimText(this HtmlHelper html, String text, Int32 trimAfter)
    //    {
    //        if (text.Length > trimAfter)
    //            return text.Remove(trimAfter) + "...";
    //        else
    //            return text;
    //    }
    //}
}